#include "trans/trans.h"

namespace iecas_ns
{
    shared_ptr<CTrans> CTrans::sm_intancePtr = nullptr;

    CTrans::CTrans()
    {
        m_commonPtr = shared_ptr<CCommon>(CCommon::getInstance());
        m_jsonUsrPtr = shared_ptr<CJsonUsr>(CJsonUsr::getInstance());
        m_trans_mesg.clear();
        m_trans_order_chg.clear();
        m_trans_order_out.clear();
    }

	shared_ptr<CTrans> CTrans::getInstance()
	{
		if(sm_intancePtr == nullptr)
		{
			sm_intancePtr = shared_ptr<CTrans>(new CTrans());
		}
		return sm_intancePtr;
	}

    bool CTrans::init(void)
    {
        if(!m_jsonUsrPtr->readFromJsonFile(TRANS_MESG_JSON_FILE_NAME, m_trans_mesg))
        {
            return false;
        }

        if(!m_jsonUsrPtr->readFromJsonFile(TRANS_ORDER_CHG_JSON_FILE_NAME, m_trans_order_chg))
        {
            return false;
        }

        if(!m_jsonUsrPtr->readFromJsonFile(TRANS_ORDER_OUT_JSON_FILE_NAME, m_trans_order_out))
        {
            return false;
        }

        return true;
    }

    void CTrans::start(void)
    {
        thread tempThread(&CTrans::loop, this);
        tempThread.detach();
    }

    void CTrans::loop(void)
    {
        while (true)
        {
            /* code */
        }
        
    }

    bool CTrans::getTransMesg(string src, vector<Value> &dest)
    {
        int i;
        for(i = 0; i < m_trans_mesg.size(); i++)
        {
            if(m_trans_mesg[i][TRANS_MESG_TAG].asString() == src)
            {
                dest.push_back(m_trans_mesg[i]);
                // cout << "get m_trans_mesg success: " << src << " -> " << m_trans_mesg[i][TRANS_MESG_DEST].asString() << endl;
                // PLOGD << "get m_trans_mesg success: " << src << " -> " << m_trans_mesg[i][TRANS_MESG_DEST].asString();
            }
        }

        i = dest.size();
        if(i)
        {
            // cout << "get m_trans_mesg : " << src << " size: " << i << endl;
            // PLOGD << "get m_trans_mesg : " << src << " size: " << i;
            return true;
        }
        else
        {
            cout << "get m_trans_mesg failed" << endl;
            PLOGD << "get m_trans_mesg failed";
            return false;
        }
    }

    bool CTrans::getTransOrderChg(string src, vector<Value> &dest)
    {
        int i;
        for(i = 0; i < m_trans_order_chg.size(); i++)
        {
            if(m_trans_order_chg[i][TRANS_ORDER_CHG_IN].asString() == src)
            {
                dest.push_back(m_trans_order_chg[i]);
                // cout << "get m_trans_order_chg success: " << src << " -> " << m_trans_order_chg[i][TRANS_ORDER_CHG_MID].asString() << endl;
                // PLOGD << "get m_trans_order_chg success: " << src << " -> " << m_trans_order_chg[i][TRANS_ORDER_CHG_MID].asString();
            }
        }

        i = dest.size();
        if(i)
        {
            // cout << "get m_trans_order_chg : " << src << " size: " << i << endl;
            // PLOGD << "get m_trans_order_chg : " << src << " size: " << i;
            return true;
        }
        else
        {
            cout << "get m_trans_order_chg failed" << endl;
            PLOGD << "get m_trans_order_chg failed";
            return false;
        }
    }

    bool CTrans::getTransOrderOut(string src, vector<Value> &dest)
    {
        int i;
        for(i = 0; i < m_trans_order_out.size(); i++)
        {
            if(m_trans_order_out[i][TRANS_ORDER_OUT_MID].asString() == src)
            {
                dest.push_back(m_trans_order_out[i]);
                // cout << "get m_trans_order_out success: " << src << " -> " << m_trans_order_out[i][TRANS_ORDER_OUT_CONT].asString() << " -> " << m_trans_order_out[i][TRANS_ORDER_OUT_DEST].asString() << endl;
                // PLOGD << "get m_trans_order_out success: " << src << " -> " << m_trans_order_out[i][TRANS_ORDER_OUT_CONT].asString() << " -> " << m_trans_order_out[i][TRANS_ORDER_OUT_DEST].asString();
            }
        }

        i = dest.size();
        if(i)
        {
            // cout << "get m_trans_order_out : " << src << " size: " << i << endl;
            // PLOGD << "get m_trans_order_out : " << src << " size: " << i;
            return true;
        }
        else
        {
            cout << "get m_trans_order_out failed" << endl;
            PLOGD << "get m_trans_order_out failed";
            return false;
        }
    }
}
